import { createRouter, createWebHistory } from 'vue-router'
import baseRouters from './modules/base'
import errRouters from './modules/error'

const routes = [...baseRouters, ...errRouters]

const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_API_PUBLIC_PATH),
  routes,
  scrollBehavior() {
    return {
      el: '#app',
      top: 0,
      behavior: 'smooth',
    }
  },
})

router.beforeEach((to, from, next) => {
  if (to.matched.length == 0) {
    document.title = '404 NOT FOUND'
    next({path: '/404'})
  } else {
    to.meta.title && (document.title = to.meta.title)
    next()
  }
})

export default router
